package com.zdjx.robot;

import com.zdjx.manage.RobotManage;
import com.zdjx.scene.BaseScene;
import com.zdjx.scene.ChatScene;
import com.zdjx.scene.RegistAndLoginScene;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Random;
import java.util.concurrent.Semaphore;

/**
 *
 */
public class RobotSence implements Runnable {

    private static Logger logger = Logger.getLogger(RobotManage.class);

    Semaphore semaphore;
    List<BaseScene> scenes;
    String gname;

    public RobotSence(Semaphore semaphore, List<BaseScene> scenes, String gname) {
        this.semaphore = semaphore;
        this.scenes = scenes;
        this.gname = gname;
    }

    @Override
    public void run() {
        while (true) {
            try {
                //尝试去阻塞的获取1个许可证
                semaphore.acquire();
                Thread.sleep(100 + new Random().nextInt(100));

                //todo
                for (BaseScene scene : scenes) {
                    if(scene instanceof RegistAndLoginScene){
                        ((RegistAndLoginScene) scene).registAndLogin(gname);
                    }
                }

            } catch (InterruptedException e) {
                logger.error("robot set up error.....");
                e.printStackTrace();
            }
        }
    }
}
